﻿Imports System.Text
Imports System.Threading

Public Class downimage


    Private connstr As String
    Private conn As New OleDb.OleDbConnection
    Private comm As New OleDb.OleDbCommand
    Private Sub Initdb()
        connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False"

        Dim filepath As String = Application.StartupPath
        connstr = String.Format(connstr, filepath + "\" + txt_connstring.Text)
        conn.ConnectionString = connstr
        comm.Connection = conn

    End Sub
    

    Private Sub btn_save_web_pic_Click_1(sender As Object, e As EventArgs) Handles btn_save_web_pic.Click
        Initdb()
        btn_save_web_pic.Enabled = False

        conn.Open()
        
        Dim sda As New OleDb.OleDbDataAdapter(txt_sql.Text.Trim, conn)
        Dim dt As New DataTable
        sda.Fill(dt)


        While (dt.Rows.Count > 0) 
            Dim url As String
            Dim title As String
            Dim sb As New StringBuilder
            lb_pic_status.Text = "共"
            Dim i As Integer = 0
            For Each dr As DataRow In dt.Rows
                ' sb.Append(txt_title_field.Text.Trim & dr(txt_title_field.Text.Trim) & ":" & dr(txt_url_field.Text.Trim) & "\n")
                url = dr(txt_url_field.Text.Trim).ToString.Trim
                title = dr(txt_title_field.Text.Trim).ToString.Trim





                If url <> "" Then

                    Dim olecom As New OleDb.OleDbCommand("update myurl set status=1 where id=" & title, conn)
                    olecom.ExecuteNonQuery()

                    Dim process As New Process()
                    process.StartInfo.FileName = "iecapt.exe"
                    If url.StartsWith("http://") Then url = url.Replace("http://", "")
                    process.StartInfo.Arguments = " --url=http://" & url & " --out=" & txt_save_path.Text.Trim & title & ".png  --min-width=1050  --max-wait=150000 --silent  --delay=5000"
                    Debug.WriteLine(process.StartInfo.Arguments)

                    process.StartInfo.UseShellExecute = False
                    process.StartInfo.RedirectStandardInput = True '可能接受来自调用程序的输入信息 
                    process.StartInfo.RedirectStandardOutput = True '由调用程序获取输出信息 
                    process.StartInfo.CreateNoWindow = True '不显示程序窗口 

                    process.Start()

                    process.WaitForExit(10000)



                    'Dim tp As New Web2Images
                    'tp.wurl = url
                    'tp.savename = title & ".png"
                    'tp.path = txt_save_path.Text.Trim

                    '' tp.Show()
                    'tp.DoWork()
                    'Threading.Thread.Sleep(100)
                    'tp = Nothing

                    'SaveImg(title)

                    Application.DoEvents()
                    i += 1
                    lb_pic_status.Text = i.ToString + "  共  " + dt.Rows.Count.ToString

                End If
            Next


            Thread.Sleep(1000)
            sda.Fill(dt)
        End While


        conn.Close()


        MsgBox("下载完成")
        btn_save_web_pic.Enabled = True



    End Sub


    Private Sub SaveImg(id As String)

        Dim sql As String = "update myurl set status='1' where id=" & id
        comm.CommandText = sql
        comm.ExecuteNonQuery()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tp As New Web2Images
        tp.wurl = TextBox1.Text.Trim()
        tp.savename = "nnhs.png"
        tp.Show()
        tp.DoWork()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim spider As mycrawler = New mycrawler
        spider.Show()
    End Sub
End Class